package com.jlf.tool.validation.constraint;


import com.jlf.tool.validation.InNumber;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

/**
 * 数字的有效性校验器
 *
 * @author wujr
 * 2021/8/27
 * @version 1.0
 * 变更历史
 * [wujr 2021/8/27 1.0 新增]
 */
public class InNumberConstraintValidator implements ConstraintValidator<InNumber, Number> {
    private InNumber rule;

    @Override
    public boolean isValid(Number number, ConstraintValidatorContext constraintValidatorContext) {
        if (number == null){
            return true;
        }
        for (long value : rule.value()){
            if (number.longValue() == value){
                return true;
            }
        }
        return false;
    }

    @Override
    public void initialize(InNumber annotation){
        this.rule = annotation;
    }
}
